﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;
using System.IO.IsolatedStorage;
using Passwords.Properties;
using System.Windows.Controls;
using System.Windows.Media;
using Passwords.Utils;
using Passwords.Views;
using System.Windows.Input;
using Passwords.Services;
using Passwords.Models;

namespace Passwords.ViewModels
{
    public class AppSettings : INotifyPropertyChanged
    {


        public AppSettings()
            : base()
        {
            PasswordsLeft = 160d;
            PasswordsRight = 400d;
            CategoriesLeft = 170d;

            LoadSettings();

        }

        private int port;
        private Window window;
        private bool autoStartService;
        private int backgroundColorIndex;
        private int accentColorIndex;
        private bool syncDeletePassword;
        private bool syncUpdatePassword;
        private bool syncDeleteCategory;
        private bool syncUpdateCategory;

        public bool SyncUpdateCategory
        {
            get { return syncUpdateCategory; }
            set
            {
                if (syncUpdateCategory != value)
                {
                    syncUpdateCategory = value;
                    OnPropertyChanged("SyncUpdateCategory");
                }
            }
        }
        

        public bool SyncDeleteCategory
        {
            get { return syncDeleteCategory; }
            set
            {
                if (syncDeleteCategory != value)
                {
                    syncDeleteCategory = value;
                    OnPropertyChanged("SyncDeleteCategory");
                }
            }
        }
        

        public bool SyncUpdatePassword
        {
            get { return syncUpdatePassword; }
            set
            {
                if (syncUpdatePassword != value)
                {
                    syncUpdatePassword = value;
                    OnPropertyChanged("SyncUpdatePassword");
                }
            }
        }
        

        public bool SyncDeletePassword
        {
            get { return syncDeletePassword; }
            set
            {
                if (syncDeletePassword != value)
                {
                    syncDeletePassword = value;
                    OnPropertyChanged("SyncDeletePassword");
                }
            }
        }
        

        public int AccentColorIndex
        {
            get { return accentColorIndex; }
            set
            {
                if (accentColorIndex != value)
                {
                    accentColorIndex = value;
                    OnPropertyChanged("AccentColorIndex");
                }
            }
        }


        public int BackgroundColorIndex
        {
            get { return backgroundColorIndex; }
            set
            {
                if (backgroundColorIndex != value)
                {
                    backgroundColorIndex = value;
                    OnPropertyChanged("BackgroundColorIndex");
                }
            }
        }

        public bool AutoStartService
        {
            get { return autoStartService; }
            set
            {
                if (autoStartService != value)
                {
                    autoStartService = value;
                    OnPropertyChanged("AutoStartService");

                }
            }
        }


        public int Port
        {
            get { return port; }
            set
            {
                if (port != value)
                {
                    port = value;
                    OnPropertyChanged("Port");
                    OnPropertyChanged("ServiceUri");
                }
            }
        }

        public string ServiceUri
        {
            get { return ServiceUtil.GetServiceUri(port); }
        }

        public Window Window
        {
            get { return window; }
            set
            {
                if (window != value)
                {
                    window = value;
                    OnPropertyChanged("Window");
                }
            }
        }

        public double PasswordsLeft { get; set; }
        public double PasswordsRight { get; set; }
        public double CategoriesLeft { get; set; }

        private void OnServiceEnabledChanged()
        {
        }


        private void LoadSettings()
        {
            var s = Settings.Default;
            PasswordsLeft = s.PasswordsLeft;
            PasswordsRight = s.PasswordsRight;
            CategoriesLeft = s.CategoriesLeft;
            BackgroundColorIndex = s.BackgroundColorIndex;
            AccentColorIndex = s.AccentColorIndex;
        }

        public void Undo()
        {
            LoadSettings();
        }

        public void SaveSettings(Window window)
        {

            Rect r = window.RestoreBounds;
            var s = Settings.Default;
            s.WindowHeight = r.Height;
            s.WindowWidth = r.Width;
            s.WindowLeft = r.Left;
            s.WindowTop = r.Top;
            s.WindowState = window.WindowState;
            s.WindowRectSet = true;
            s.AccentColorIndex = AccentColorIndex;
            s.BackgroundColorIndex = BackgroundColorIndex;
            s.SyncDeletePassword = SyncDeletePassword;
            s.SyncUpdatePassword = SyncUpdatePassword;
            s.SyncDeleteCategory = SyncDeleteCategory;
            s.SyncUpdateCategory = SyncUpdateCategory;


            s.IsServiceEnabled = AutoStartService;



            foreach (UserControl uc in window.GetVisualDescendants().OfType<UserControl>())
            {
                IConfigSetter setter = uc as IConfigSetter;
                if (setter != null) setter.SaveSettings();
            }


            s.PasswordsLeft = PasswordsLeft;
            s.PasswordsRight = PasswordsRight;
            s.CategoriesLeft = CategoriesLeft;
            s.Port = port;

            SetTheme();

            s.Save();
        }


        public void SetTheme()
        {
            SetColor("AppWindowBackground");
            SetColor("AppForeground");
            SetColor("AppBackground");
            SetColor("AppChromeBrush");
            SetColor("AppMediumBrush");
            SetColor("AppTextBoxBrush");
            SetColor("focusBitmapEffect");

            AccentColor accentColor = AccentColor.SysForegroundColors[accentColorIndex];

            App.Current.Resources["AppAccentBrush"] = new SolidColorBrush(accentColor.Color);
            //(App.Current.Resources["AppAccentBrush"] as SolidColorBrush).Color = accentColor.Color;
        }

        private void SetColor(string n)
        {
            string p = backgroundColorIndex == 0 ? "D" : "L";
            var res = App.Current.Resources;
            res[n] = res[p + n];
        }

        public void LoadSettings(Window window)
        {
            var s = Settings.Default;
            if (s.WindowRectSet)
            {
                double w = s.WindowWidth;
                double h = s.WindowHeight;
                double l = s.WindowLeft;
                double t = s.WindowTop;

                if (!double.IsNaN(w)) window.Width = w;
                if (!double.IsNaN(h)) window.Height = h;
                if (!double.IsNaN(t) && t != -1d) window.Top = t;
                if (!double.IsNaN(l) && t != -1d) window.Left = l;
                window.WindowState = s.WindowState;
            }
            AutoStartService = s.IsServiceEnabled;
            Port = s.Port;
            SyncDeletePassword = s.SyncDeletePassword;
            SyncUpdatePassword = s.SyncUpdatePassword;
            SyncDeleteCategory = s.SyncDeleteCategory;
            SyncUpdateCategory = s.SyncUpdateCategory;
        }

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler eh = PropertyChanged;
            if (eh != null) eh(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
